package com.openbravo.pos.printer;

import com.openbravo.data.loader.LocalRes;
import com.openbravo.pos.forms.DataLogicSystem;
import java.applet.Applet;
import java.applet.AudioClip;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.Reader;
import java.io.StringReader;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.Attributes;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

public class TicketParser extends DefaultHandler
{
  private static SAXParser m_sp = null;
  private DeviceTicket m_printer;
  private DataLogicSystem m_system;
  private StringBuffer text;
  private String bctype;
  private String bcposition;
  private int m_iTextAlign;
  private int m_iTextLength;
  private int m_iTextStyle;
  private StringBuffer m_sVisorLine;
  private int m_iVisorAnimation;
  private String m_sVisorLine1;
  private String m_sVisorLine2;
  private double m_dValue1;
  private double m_dValue2;
  private int attribute3;
  private int m_iOutputType;
  private static final int OUTPUT_NONE = 0;
  private static final int OUTPUT_DISPLAY = 1;
  private static final int OUTPUT_TICKET = 2;
  private static final int OUTPUT_FISCAL = 3;
  private DevicePrinter m_oOutputPrinter;

  public TicketParser(DeviceTicket paramDeviceTicket, DataLogicSystem paramDataLogicSystem)
  {
    this.m_printer = paramDeviceTicket;
    this.m_system = paramDataLogicSystem;
  }

  public void printTicket(String paramString)
    throws TicketPrinterException
  {
    printTicket(new StringReader(paramString));
  }

  public void printTicket(Reader paramReader)
    throws TicketPrinterException
  {
    try
    {
      if (m_sp == null)
      {
        SAXParserFactory localSAXParserFactory = SAXParserFactory.newInstance();
        m_sp = localSAXParserFactory.newSAXParser();
      }
      m_sp.parse(new InputSource(paramReader), this);
    }
    catch (ParserConfigurationException localParserConfigurationException)
    {
      throw new TicketPrinterException(LocalRes.getIntString("exception.parserconfig"), localParserConfigurationException);
    }
    catch (SAXException localSAXException)
    {
      throw new TicketPrinterException(LocalRes.getIntString("exception.xmlfile"), localSAXException);
    }
    catch (IOException localIOException)
    {
      throw new TicketPrinterException(LocalRes.getIntString("exception.iofile"), localIOException);
    }
  }

  public void startDocument()
    throws SAXException
  {
    this.text = null;
    this.bctype = null;
    this.bcposition = null;
    this.m_sVisorLine = null;
    this.m_iVisorAnimation = 0;
    this.m_sVisorLine1 = null;
    this.m_sVisorLine2 = null;
    this.m_iOutputType = 0;
    this.m_oOutputPrinter = null;
  }

  public void endDocument()
    throws SAXException
  {
  }

  public void startElement(String paramString1, String paramString2, String paramString3, Attributes paramAttributes)
    throws SAXException
  {
    String str;
    switch (this.m_iOutputType)
    {
    case 0:
      if ("opendrawer".equals(paramString3))
      {
        this.m_printer.getDevicePrinter(readString(paramAttributes.getValue("printer"), "1")).openDrawer();
      }
      else if ("play".equals(paramString3))
      {
        this.text = new StringBuffer();
      }
      else if ("ticket".equals(paramString3))
      {
        this.m_iOutputType = 2;
        this.m_oOutputPrinter = this.m_printer.getDevicePrinter(readString(paramAttributes.getValue("printer"), "1"));
        this.m_oOutputPrinter.beginReceipt();
      }
      else if ("display".equals(paramString3))
      {
        this.m_iOutputType = 1;
        str = paramAttributes.getValue("animation");
        if ("scroll".equals(str))
          this.m_iVisorAnimation = 2;
        else if ("flyer".equals(str))
          this.m_iVisorAnimation = 1;
        else if ("blink".equals(str))
          this.m_iVisorAnimation = 3;
        else if ("curtain".equals(str))
          this.m_iVisorAnimation = 4;
        else
          this.m_iVisorAnimation = 0;
        this.m_sVisorLine1 = null;
        this.m_sVisorLine2 = null;
        this.m_oOutputPrinter = null;
      }
      else if ("fiscalreceipt".equals(paramString3))
      {
        this.m_iOutputType = 3;
        this.m_printer.getFiscalPrinter().beginReceipt();
      }
      else if ("fiscalzreport".equals(paramString3))
      {
        this.m_printer.getFiscalPrinter().printZReport();
      }
      else
      {
        if (!"fiscalxreport".equals(paramString3))
          break;
        this.m_printer.getFiscalPrinter().printXReport();
      }
      break;
    case 2:
      if ("image".equals(paramString3))
      {
        this.text = new StringBuffer();
      }
      else if ("barcode".equals(paramString3))
      {
        this.text = new StringBuffer();
        this.bctype = paramAttributes.getValue("type");
        this.bcposition = paramAttributes.getValue("position");
      }
      else if ("line".equals(paramString3))
      {
        this.m_oOutputPrinter.beginLine(parseInt(paramAttributes.getValue("size"), 0));
      }
      else
      {
        if (!"text".equals(paramString3))
          break;
        this.text = new StringBuffer();
        this.m_iTextStyle = (("true".equals(paramAttributes.getValue("bold")) ? 1 : 0) | ("true".equals(paramAttributes.getValue("underline")) ? 2 : 0));
        str = paramAttributes.getValue("align");
        if ("right".equals(str))
          this.m_iTextAlign = 1;
        else if ("center".equals(str))
          this.m_iTextAlign = 2;
        else
          this.m_iTextAlign = 0;
        this.m_iTextLength = parseInt(paramAttributes.getValue("length"), 0);
      }
      break;
    case 1:
      if ("line".equals(paramString3))
      {
        this.m_sVisorLine = new StringBuffer();
      }
      else if ("line1".equals(paramString3))
      {
        this.m_sVisorLine = new StringBuffer();
      }
      else if ("line2".equals(paramString3))
      {
        this.m_sVisorLine = new StringBuffer();
      }
      else
      {
        if (!"text".equals(paramString3))
          break;
        this.text = new StringBuffer();
        str = paramAttributes.getValue("align");
        if ("right".equals(str))
          this.m_iTextAlign = 1;
        else if ("center".equals(str))
          this.m_iTextAlign = 2;
        else
          this.m_iTextAlign = 0;
        this.m_iTextLength = parseInt(paramAttributes.getValue("length"));
      }
      break;
    case 3:
      if ("line".equals(paramString3))
      {
        this.text = new StringBuffer();
        this.m_dValue1 = parseDouble(paramAttributes.getValue("price"));
        this.m_dValue2 = parseDouble(paramAttributes.getValue("units"), 1.0D);
        this.attribute3 = parseInt(paramAttributes.getValue("tax"));
      }
      else if ("message".equals(paramString3))
      {
        this.text = new StringBuffer();
      }
      else
      {
        if (!"total".equals(paramString3))
          break;
        this.text = new StringBuffer();
        this.m_dValue1 = parseDouble(paramAttributes.getValue("paid"));
      }
    }
  }

  public void endElement(String paramString1, String paramString2, String paramString3)
    throws SAXException
  {
    switch (this.m_iOutputType)
    {
    case 0:
      if (!"play".equals(paramString3))
        break;
      try
      {
        AudioClip localAudioClip = Applet.newAudioClip(getClass().getClassLoader().getResource(this.text.toString()));
        localAudioClip.play();
      }
      catch (Exception localException1)
      {
      }
      this.text = null;
      break;
    case 2:
      if ("image".equals(paramString3))
      {
        try
        {
          BufferedImage localBufferedImage = this.m_system.getResourceAsImage(this.text.toString());
          if (localBufferedImage != null)
            this.m_oOutputPrinter.printImage(localBufferedImage);
        }
        catch (Exception localException2)
        {
        }
        this.text = null;
      }
      else if ("barcode".equals(paramString3))
      {
        this.m_oOutputPrinter.printBarCode(this.bctype, this.bcposition, this.text.toString());
        this.text = null;
      }
      else if ("text".equals(paramString3))
      {
        if (this.m_iTextLength > 0)
          switch (this.m_iTextAlign)
          {
          case 1:
            this.m_oOutputPrinter.printText(this.m_iTextStyle, DeviceTicket.alignRight(this.text.toString(), this.m_iTextLength));
            break;
          case 2:
            this.m_oOutputPrinter.printText(this.m_iTextStyle, DeviceTicket.alignCenter(this.text.toString(), this.m_iTextLength));
            break;
          default:
            this.m_oOutputPrinter.printText(this.m_iTextStyle, DeviceTicket.alignLeft(this.text.toString(), this.m_iTextLength));
            break;
          }
        else
          this.m_oOutputPrinter.printText(this.m_iTextStyle, this.text.toString());
        this.text = null;
      }
      else if ("line".equals(paramString3))
      {
        this.m_oOutputPrinter.endLine();
      }
      else
      {
        if (!"ticket".equals(paramString3))
          break;
        this.m_oOutputPrinter.endReceipt();
        this.m_iOutputType = 0;
        this.m_oOutputPrinter = null;
      }
      break;
    case 1:
      if ("line".equals(paramString3))
      {
        if (this.m_sVisorLine1 == null)
          this.m_sVisorLine1 = this.m_sVisorLine.toString();
        else
          this.m_sVisorLine2 = this.m_sVisorLine.toString();
        this.m_sVisorLine = null;
      }
      else if ("line1".equals(paramString3))
      {
        this.m_sVisorLine1 = this.m_sVisorLine.toString();
        this.m_sVisorLine = null;
      }
      else if ("line2".equals(paramString3))
      {
        this.m_sVisorLine2 = this.m_sVisorLine.toString();
        this.m_sVisorLine = null;
      }
      else if ("text".equals(paramString3))
      {
        if (this.m_iTextLength > 0)
          switch (this.m_iTextAlign)
          {
          case 1:
            this.m_sVisorLine.append(DeviceTicket.alignRight(this.text.toString(), this.m_iTextLength));
            break;
          case 2:
            this.m_sVisorLine.append(DeviceTicket.alignCenter(this.text.toString(), this.m_iTextLength));
            break;
          default:
            this.m_sVisorLine.append(DeviceTicket.alignLeft(this.text.toString(), this.m_iTextLength));
            break;
          }
        else
          this.m_sVisorLine.append(this.text);
        this.text = null;
      }
      else
      {
        if (!"display".equals(paramString3))
          break;
        this.m_printer.getDeviceDisplay().writeVisor(this.m_iVisorAnimation, this.m_sVisorLine1, this.m_sVisorLine2);
        this.m_iVisorAnimation = 0;
        this.m_sVisorLine1 = null;
        this.m_sVisorLine2 = null;
        this.m_iOutputType = 0;
        this.m_oOutputPrinter = null;
      }
      break;
    case 3:
      if ("fiscalreceipt".equals(paramString3))
      {
        this.m_printer.getFiscalPrinter().endReceipt();
        this.m_iOutputType = 0;
      }
      else if ("line".equals(paramString3))
      {
        this.m_printer.getFiscalPrinter().printLine(this.text.toString(), this.m_dValue1, this.m_dValue2, this.attribute3);
        this.text = null;
      }
      else if ("message".equals(paramString3))
      {
        this.m_printer.getFiscalPrinter().printMessage(this.text.toString());
        this.text = null;
      }
      else
      {
        if (!"total".equals(paramString3))
          break;
        this.m_printer.getFiscalPrinter().printTotal(this.text.toString(), this.m_dValue1);
        this.text = null;
      }
    }
  }

  public void characters(char[] paramArrayOfChar, int paramInt1, int paramInt2)
    throws SAXException
  {
    if (this.text != null)
      this.text.append(paramArrayOfChar, paramInt1, paramInt2);
  }

  private int parseInt(String paramString, int paramInt)
  {
    try
    {
      return Integer.parseInt(paramString);
    }
    catch (NumberFormatException localNumberFormatException)
    {
    }
    return paramInt;
  }

  private int parseInt(String paramString)
  {
    return parseInt(paramString, 0);
  }

  private double parseDouble(String paramString, double paramDouble)
  {
    try
    {
      return Double.parseDouble(paramString);
    }
    catch (NumberFormatException localNumberFormatException)
    {
    }
    return paramDouble;
  }

  private double parseDouble(String paramString)
  {
    return parseDouble(paramString, 0.0D);
  }

  private String readString(String paramString1, String paramString2)
  {
    if ((paramString1 == null) || (paramString1.equals("")))
      return paramString2;
    return paramString1;
  }
}

/* Location:           D:\PowerFulPOS-2.30.2\openbravopos.jar
 * Qualified Name:     com.openbravo.pos.printer.TicketParser
 * JD-Core Version:    0.6.0
 */